#ifndef _UI_HPP
#define _UI_HPP


#define LV_TICK_PERIOD_MS 100

// 页面显示
lv_obj_t *pageMain;
lv_obj_t *pageInfo;
lv_obj_t *pageHome;

// 当前页面
int *currentPage = 0;

void uiInitTask();
void hidePage(int pageNum);
void showPage(int pageNum);
void changePage(int oldPageNum, int newPageNum);
// void cleanPageMain();
static void lvTickTask();


#define PAGE_CNT 2

// typedef struct _page_t{
// 	lv_obj_t *pageObj;
// 	int page;
// 	bool show;
// } page_t;

// page_t pageGroup[PAGE_CNT];

#include "font/cfont_3.h"
#include "common/ui_color.h"
#include "module/ui_top_status_bar.h"
#include "pages/page_init.h"
#include "pages/page_home.h"
#include "pages/page_info.h"
#include "pages/page_test.h"


// static void initPageGroup();

// static void initPageGroup() {
// 	page_t pageGroup[PAGE_CNT] = {
// 		{pageInfo, PAGE_INFO, false},
// 		{pageHome, PAGE_HOME, false}
// 	};
// }
// void cleanPageMain() {
// 	lv_obj_clean(pageMain);
// }

void showPage(int pageNum) {
	currentPage = pageNum;
	// pageGroup[pageNum].show = true;
	// if(lv_obj_has_flag(pageGroup[page].pageObj, LV_OBJ_FLAG_HIDDEN)) {
		// lv_obj_clear_flag(pageGroup[page].pageObj, LV_OBJ_FLAG_HIDDEN);
	// }
}
void hidePage(int pageNum) {
	// if(!lv_obj_has_flag(pageGroup[page].pageObj, LV_OBJ_FLAG_HIDDEN)) {
	// 	lv_obj_add_flag(pageGroup[page].pageObj, LV_OBJ_FLAG_HIDDEN);
	// }
}
void changePage(int oldPageNum, int newPageNum) {
	hidePage(oldPageNum);
	showPage(newPageNum);
}



void uiInitTask() {
	lv_init();
	lvgl_driver_init();
	// 320 * 40
	lv_color_t *lvglBuf_1 = heap_caps_malloc(DISP_BUF_SIZE * sizeof(lv_color_t), MALLOC_CAP_DMA);
	// lv_color_t *lvglBuf_1 = heap_caps_malloc(25600 * sizeof(lv_color_t), MALLOC_CAP_DMA);
	assert(lvglBuf_1 != NULL);
	lv_color_t *lvglBuf_2 = heap_caps_malloc(DISP_BUF_SIZE * sizeof(lv_color_t), MALLOC_CAP_DMA);
	// lv_color_t *lvglBuf_2 = heap_caps_malloc(25600 * sizeof(lv_color_t), MALLOC_CAP_DMA);
	assert(lvglBuf_2 != NULL);

	static lv_disp_draw_buf_t dispBuf;
	uint32_t size_in_px = DISP_BUF_SIZE;

	lv_disp_draw_buf_init(&dispBuf, lvglBuf_1, lvglBuf_2, size_in_px);
	lv_disp_drv_t dispDrv;
	lv_disp_drv_init(&dispDrv);
	dispDrv.flush_cb = disp_driver_flush;
	dispDrv.draw_buf = &dispBuf;
	dispDrv.hor_res = LV_HOR_RES_MAX;
	dispDrv.ver_res = LV_VER_RES_MAX;
	lv_disp_drv_register(&dispDrv);

	// 触摸
	// lv_indev_drv_t indev_drv;
	// lv_indev_drv_init(&indev_drv);
	// indev_drv.read_cb = touch_driver_read;
	// indev_drv.type = LV_INDEV_TYPE_POINTER;
	// lv_indev_drv_register(&indev_drv);

	const esp_timer_create_args_t periodic_timer_args = {
		.callback = &lvTickTask,
		.name = "periodic_gui",
	};
	esp_timer_handle_t periodic_timer;
	ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
	ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, LV_TICK_PERIOD_MS * 1000));

	// initPageGroup();

	xEventGroupSetBits(eventGroup, EVENT_SCREEN_INIT_DONE);

	while (1) {
		lv_timer_handler();
		vTaskDelay(pdMS_TO_TICKS(100));
	}

	free(lvglBuf_1);
	free(lvglBuf_2);

	vTaskDelete(NULL);    
}
void lvTickTask() {
	lv_tick_inc(LV_TICK_PERIOD_MS);
}

#endif